#include "car.h"

car::car(sc_module_name name) : sc_module(name)
{
    SC_THREAD(husband_thread);
    SC_THREAD(wife_thread);
}

void car::husband_thread(void)
{
    while (true)
    {
        if (driver_seat.trylock() == 0) {
            std::cout << sc_time_stamp() << " Husband: I'm driving the car." << std::endl;
            wait(10, SC_SEC);
            std::cout << sc_time_stamp() << " Husband: I'm finish driving." << std::endl;
            driver_seat.unlock();
        }
        std::cout << sc_time_stamp() << "   Husband: I want driving the car." << std::endl;
        wait(10, SC_SEC);
    }
}

void car::wife_thread(void)
{
    while (true)
    {
        std::cout << sc_time_stamp() << "   Wife: I want driving the car." << std::endl;
        if (driver_seat.trylock() == 0)
        {
            std::cout << sc_time_stamp() << "   Wife: Now, I'm driving the car." << std::endl;
            wait(20, SC_SEC);
            std::cout << sc_time_stamp() << "   Wife: I'm finish driving." << std::endl;
            driver_seat.unlock();
        }
        wait(5, SC_SEC);
    }
}